<?php
require_once("Includes/connection.php");
require_once("Includes/functions.php");
session_start();
if(isset($_SESSION['username'])){
	if(isset($_POST['submit'])){
		if(!isset($_POST['start'])){
			$start=date("Y-m-d H:i:00");
		}
		else{
			$start=$_POST['start'];
		}
		if(!isset($_POST['end'])){
			$val1=date("Y-m-d");
			$val2=date("H")+2;
			$val3=date(":i:00");
			$end=$val1.$val2.$val3;
		}
		else{
			$end=$_POST['end'];
		}
		if(!empty($_POST['start']) && !empty($_POST['end']) && !empty($_POST['name']) && !empty($_POST['short_name'])){
			$query = sprintf("SELECT short_name FROM polls WHERE short_name='%s'",mysql_real_escape_string($_POST['short_name']));
			$result = mysql_query($query);		
			if (mysql_num_rows($result) == 0){
				$query = sprintf("SELECT name FROM polls WHERE name='%s'",mysql_real_escape_string($_POST['name']));
				$result = mysql_query($query);		
				if (mysql_num_rows($result) == 0){
					$query = sprintf("INSERT INTO polls (type, time_start, time_end, name, short_name, registration_required, multiple_entries, sms_entries, web_entries) 
					VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')",
					mysql_real_escape_string($_POST['type']), mysql_real_escape_string($_POST['start']), mysql_real_escape_string($_POST['end']), 
					mysql_real_escape_string($_POST['name']),	mysql_real_escape_string($_POST['short_name']), mysql_real_escape_string($_POST['registration']), 
					mysql_real_escape_string($_POST['multiple']), mysql_real_escape_string($_POST['sms']), mysql_real_escape_string($_POST['web']));
					mysql_query($query);
					$name='';
					$short_name='';
					$error="The poll was successfully created!";
				}
				else{
					$name='';
					$short_name='';
					$error="Name already exist in other poll.";
				}
			}	
			else{
				$name='';
				$short_name='';
				$error="Short name already exist in other poll.";
			}		
		}
		else{
			$name='';
			$short_name='';
			$error="Please fill all fields";
		}
	}
	else{
		$start=date("Y-m-d H:i:00");
		$val1=date("Y-m-d ");
		$val2=date("H")+2;
		if(count($val2)==1){
			$val2='0'.$val2;
		}
		$val3=date(":i:00");
		$end=$val1.$val2.$val3;
		$name='';
		$short_name='';
	}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="css/main.css" />
<title>Q&A </title>
</head>
<body>
	<table id="header">
    <tr><td></td>
    <td colspan="2" style="width:1000px; text-align:center;">
    	<table><tr><td width="180">
    	<img src="images/logo_qanda.gif" height="80"/>
		</td><td>
        <h1 style="height:auto;">ACM RUM Q&A </h1>
		</td></tr></table>
    </td>
    <td></td></tr>
    <tr><td style="border-bottom:1px solid black;"></td><td style="width:180px;"></td><td style="border-bottom:1px solid black; width:813px;"></td><td style="border-bottom:1px solid black;"></td></tr>
    </table>
    <table id="container">
        <tr>
        <td style="border-left:1px solid black; border-right:1px solid black; width:176px; height:30px; padding-top:10px; padding-left:5px; background-color:#FC0;">
        <a href="home.php">Home</a>
        </td><td rowspan="6" style="border-right:1px solid #CCC; border-bottom:1px solid #CCC; padding-top:15px;">
        	<form action="create_poll.php" method="post">
            <table id="main_content">
            <tr>
            <td>Type: </td><td><select name="type"><option value="quiz">Quiz</option><option value="open">Open</option></select></td>
            </tr><tr>
            <td>Start: </td><td><input type="text" name="start" maxlength="100" size="30" value="<?php echo htmlentities($start); ?>"/></td>
            </tr><tr>
            <td>End: </td><td><input type="text" name="end" maxlength="100" size="30" value="<?php echo htmlentities($end); ?>"/></td>
            </tr><tr>
            <td>Name: </td><td><input type="text" name="name" maxlength="100" size="30" value="<?php echo htmlentities($name); ?>"/></td>
            </tr><tr>
            <td>Short Name: </td><td><input type="text" name="short_name" maxlength="100" size="30" value="<?php echo htmlentities($short_name); ?>"/></td>
            </tr><tr>
            <td>Registration Required: </td><td><select name="registration"><option value="no">No</option><option value="yes">Yes</option></select></td>
            </tr><tr>
            <td>Multiple Entries: </td><td><select name="multiple"><option value="no">No</option><option value="yes">Yes</option></select></td>
            </tr><tr>
            <td>SMS Entries: </td><td><select name="sms"><option value="yes">Yes</option><option value="no">No</option></select></td>
            </tr><tr>
            <td>Web Entries: </td><td><select name="web"><option value="yes">Yes</option><option value="no">No</option></select></td>
			</tr><tr>
            <td><input type="submit" name="submit" value="Create" /></td>
            </tr>
            <?php
			if(isset($error)){
				echo '<tr><td colspan="2">'.$error.'</td></tr>';
			}
			?>
            </table>
            </form>
        </td></tr>
        <tr><td style="border:1px solid black; border-right:none; background-color:#FFF; width:176px; height:30px; padding-left:5px;">
        <a href="create_poll.php">Create Poll</a>
        </td></tr>
        <tr><td style="border-left:1px solid black; border-right:1px solid black; width:176px; height:30px; padding-left:5px; background-color:#FC0;">
        <a href="view_polls.php">View Polls</a>
        </td></tr>
        <tr><td style="border-left:1px solid black; border-right:1px solid black; width:176px; height:30px; padding-left:5px; background-color:#FC0;">
        <a href="manage_accounts.php">Manage Accounts</a>
        </td></tr>
        <tr><td style="border-left:1px solid black; border-right:1px solid black; width:176px; height:30px; padding-left:5px; background-color:#FC0;">
        <a href="index.php">Logout</a>
        </td></tr>
        <tr><td style="border:1px solid black; border-top:none; width:176px; padding-left:5px; background-color:#FC0;">
        </td>
        </tr>
    </table>
</body>
</html>
<?php 
}
else{
	header('Location: index.php');
}
?>